﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;
using GE_Management_Tool;

namespace Gem_Tools.Planning
{
    public partial class frmA0011_Project_Edit_Page : frmBase, ICategory<CProject>
    {
        CProject m_objProject;
        IList<CClient> m_lsClient;
        Boolean m_is_new = false;

        public frmA0011_Project_Edit_Page()
        {
            InitializeComponent();
            CCommon_Function.Format_All(this);
        }

        private void frmA0011_Project_Edit_Page_Load(object sender, EventArgs e)
        {
            try
            {
                m_lsClient = new List<CClient>();
                CClient_Controller v_CtrlClient = new CClient_Controller();
                m_lsClient = v_CtrlClient.List_Client();

                lkeClient.Properties.DataSource = m_lsClient;

                if (m_is_new == false)
                {
                    Assign_Obj_To_Control(m_objProject);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        public CProject Show_To_Create()
        {
            m_is_new = true;
            this.Text = "Create New Project";
            this.ShowDialog();

            return m_objProject;
        }

        public CProject Show_To_Edit(CProject pobjData, ref bool pisChange)
        {
            m_is_new = false;
            chkAgreement.Visible = false;
            this.Text = "Update Project " + pobjData.Name;
            m_objProject = pobjData;
            this.ShowDialog();

            return m_objProject;
        }

        public void Assign_Control_To_Obj()
        {
            m_objProject.Name = txtProjectName.Text.Trim();
            m_objProject.Client_ID = int.Parse(lkeClient.EditValue.ToString());
            m_objProject.Start_Date = dtmStartDate.DateTime;
            m_objProject.End_Date = dtmEndDate.DateTime;
            m_objProject.Manday = int.Parse(txtManday.EditValue.ToString());
            m_objProject.Note = txtProjectNote.Text.Trim();
        }

        public void Assign_Obj_To_Control(CProject pobjData)
        {
            txtProjectName.Text = pobjData.Name;
            lkeClient.EditValue = pobjData.Client_ID;
            dtmStartDate.DateTime = pobjData.Start_Date;
            dtmEndDate.DateTime = pobjData.End_Date;
            txtManday.Value = pobjData.Manday;
            txtProjectNote.Text = pobjData.Note;
        }

        public string Check_Before_Save()
        {
            StringBuilder v_sb = new StringBuilder();
            if (txtProjectName.Text == "")
            {
                v_sb.AppendLine("-Missing Project Name.");
            }
            if (dtmStartDate.Text == "")
            {
                v_sb.AppendLine("-Missing Start Date.");
            }
            if (dtmEndDate.Text == "")
            {
                v_sb.AppendLine("-Missing End Date.");
            }
            if (lkeClient.EditValue == null)
            {
                v_sb.AppendLine("-Missing Client Info.");
            }

            if (chkAgreement.Checked == false && m_is_new == true)
            {
                v_sb.AppendLine("please choose agree to create");
            }
            return v_sb.ToString();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                string error = Check_Before_Save();
                if (error != "")
                {
                    throw new Exception(error);
                }

                if (m_is_new == true)
                {
                    m_objProject = new CProject();
                    Assign_Control_To_Obj();
                    CProject_Controller v_objCtrlProject = new CProject_Controller();
                    m_objProject.Auto_ID = v_objCtrlProject.Insert_Project(m_objProject);
                }
                else
                {

                    Assign_Control_To_Obj();
                    CProject_Controller v_objCtrlProject = new CProject_Controller();
                    v_objCtrlProject.Update_Project(m_objProject);
                }

                this.Close();
                XtraMessageBox.Show("Save Success !");
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }
    }
}